﻿using System;
using System.Collections.Generic;
using System.Text;
using Musicman.Common;
using Musicman.DAL;
using Musicman.Common.Entities;

namespace Musicman.Business
{
    public class SearchJobManager
    {
        # region singleton definition
        static readonly SearchJobManager instance = new SearchJobManager();

        public static SearchJobManager Instance
        {
            get
            {
                return instance;
            }
        }
        static SearchJobManager()
        {
        }
        # endregion

        #region select

        public long GetSearchJobId(string jobName, string criteria, byte jobType)
        {
            return GetSearchJobId(jobName, criteria, jobType, true);
        }

        /// <summary>
        /// Retreives searchJobId from datbase for given values in config
        /// </summary>
        /// <param name="jobName"></param>
        /// <param name="criteria"></param>
        /// <param name="jobType"></param>
        /// <param name="createIfEmpty"></param>
        /// <returns></returns>
        public long GetSearchJobId(string jobName, string criteria, byte jobType, bool createIfEmpty)
        {
            long res = -1;
            try
            {
                res = SearchJobDAO.Instance.GetSearchJobID(jobName);
                if (res == -1)
                {
                    SearchJob job = new SearchJob();
                    job.Name = jobName;
                    job.AllCriteria = criteria;
                    job.Type = jobType;
                    if (createIfEmpty)
                    {
                        SearchJobDAO.Instance.InsertSearchJob(job);
                        res = job.SearchJobID;
                    }
                }
            }
            catch (Exception ex)
            {
                LogManager.LogException("Unable to get searchjob ID", ex);
            }
            return res;
        }

        public List<SearchJob> LoadAllSearchJobs()
        {
            List<SearchJob> res = null;
            try
            {
                res = SearchJobDAO.Instance.LoadSeardhJobs("", "ORDER BY SearchJob.StartTime", new SelectParameter[] { });
            }
            catch (Exception ex)
            {
                LogManager.LogException("Unable to load searchjobs", ex);
            }
            return res;
        }
        #endregion

        #region update 
        public bool UpdateSearchJob(long jobid, DateTime startdate, DateTime enddate)
        {
            bool res = false;
            try
            {
                res = SearchJobDAO.Instance.UpdateSearchJob(jobid, startdate, enddate); 
            }
            catch (Exception ex)
            {
                LogManager.LogException("Unable to load searchjobs", ex);
            }
            return res;
        }

        #endregion

        #region delete
        /// <summary>
        /// Clears files that doesn't belong to any active search job in config
        /// </summary>
        /// <param name="acitveJobIds"></param>
        /// <returns></returns>
        public bool ClearInactiveJobs(List<long> acitveJobIds)
        {
            bool res = true;
            try
            {
                List<long> inactiveJobIds = SearchJobDAO.Instance.GetInactiveJobIds(acitveJobIds);
                for (int i = 0; i < inactiveJobIds.Count; i++)
                {
                    SearchJobDAO.Instance.ClearJobData(inactiveJobIds[i]);
                    res = res & SearchJobDAO.Instance.DeleteSearchJob(inactiveJobIds[i]);
                }
            }
            catch (Exception ex)
            {
                res = false;
                LogManager.LogException("Unable to clear hanging files", ex);
            }
            return res;
        }
        #endregion
    }
}
